package cn.oms.esb.provider.service.impl;


import cn.oms.esb.api.dto.req.SendEmailReq;
import cn.oms.esb.api.service.IEmailService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RestController;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

@RestController
@Service
public class EmailServiceImpl implements IEmailService {

    @Autowired
    private JavaMailSender javaMailSender;

    @Value("${spring.mail.username}")
    private String from;

    /**
     * 外部邮件发送接口
     */
    @Override
    public String sendEmail(SendEmailReq sendEmailReq) {

        // 1. 创建一个MimeMessage
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();

        // 2.发送邮件的一个工具类
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage);

        try {
            // 设置邮件的基本内容
            mimeMessageHelper.setSubject(sendEmailReq.getTitle()); // 邮件标题
            mimeMessageHelper.setText(sendEmailReq.getContent(),true); // 邮件内容
            mimeMessageHelper.setFrom(from);    // 发件人
            mimeMessageHelper.setTo(sendEmailReq.getTo()); // 收件人
            javaMailSender.send(mimeMessage); //发送
            return "SUCCESS";
        } catch (MessagingException e) {
            e.printStackTrace();
            return "FALSE";
        }
    }

    /**
     *
     * 向指定邮箱发送验证码
     * @param email
     * @return
     */
    @Override
    public String sendVerificationCode(String email,String templateType) {

        // 获取邮件模板


        return null;
    }

}
